package com.baykalcakici.cafepicker;

import java.util.concurrent.ExecutionException;

import com.baykalcakici.cafepicker.listener.PreviousMoveAnimationListener;
import com.baykalcakici.cafepicker.listener.VisitDialogListener;
import com.baykalcakici.cafepicker.tasks.LoginTask;
import com.baykalcakici.cafepicker.tasks.PickTask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

/**
 * Pick cafe tab.
 */
public class PickCafe extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pick_cafe);

    final TextView currentPickTextView = (TextView) findViewById(R.id.currentPickText);
    final TextView currentProbTextView = (TextView) findViewById(R.id.currentProbText);

    final TextView previousPickTextView = (TextView) findViewById(R.id.previousPickText);
    final TextView previousProbTextView = (TextView) findViewById(R.id.previousProbText);

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        PickTask pickTask = new PickTask();
        pickTask.execute();
        Pair<String, Double> result = null;
        try {
          result = pickTask.get();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (ExecutionException e) {
          e.printStackTrace();
        }

        if (result != null) {
          Animation animationPick =
              AnimationUtils.loadAnimation(PickCafe.this, R.anim.prev_pick_move_up);
          Animation animationProb =
              AnimationUtils.loadAnimation(PickCafe.this, R.anim.prev_pick_move_up);
          animationPick.setAnimationListener(new PreviousMoveAnimationListener(currentPickTextView,
              previousPickTextView, result.first));
          animationProb.setAnimationListener(new PreviousMoveAnimationListener(currentProbTextView,
              previousProbTextView, String.format("probability: %4.3f", result.second)));

          previousPickTextView.clearAnimation();
          previousProbTextView.clearAnimation();
          previousPickTextView.startAnimation(animationPick);
          previousProbTextView.startAnimation(animationProb);

          currentPickTextView.setOnClickListener(new VisitDialogListener(currentPickTextView,
              PickCafe.this));
          previousPickTextView.setOnClickListener(new VisitDialogListener(previousPickTextView,
              PickCafe.this));
        } else {
          ProgressDialog loginDialog =
              ProgressDialog.show(PickCafe.this, "", "Initializing. Please wait...", true);
          LoginTask loginTask = new LoginTask(loginDialog, PickCafe.this);
          loginTask.execute();
        }
      }
    });
  }
}
